package com.zrsx.managemodel.controller;

import com.zrsx.managemodel.common.dto.CreditDTO;
import com.zrsx.managemodel.common.dto.CreditDimensionDTO;
import com.zrsx.managemodel.common.dto.KafkaDTO;
import com.zrsx.managemodel.common.dto.RecordDTO;
import com.zrsx.managemodel.common.entity.CreditInterpretation;
import com.zrsx.managemodel.common.result.Result;
import com.zrsx.managemodel.service.CreditManageService;
import com.zrsx.managemodel.service.RecordService;
import com.zrsx.managemodel.service.serviceImpl.OpenFeignService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/creditManage")
@Slf4j
@Tag(name = "信用分管理")
public class CreditManageController {
    @Autowired
    private CreditManageService creditManageService;
    @Autowired
    private OpenFeignService openFeignService;
    @Autowired
    private RecordService recordService;


    /**
     * 新增用户信用分
     * @param creditDTO
     * @return
     */
    @PostMapping("/credit")
    @Operation(summary = "新增用户信用分")
    public Result<String> addCredit(@RequestBody CreditDTO creditDTO){
        System.out.println("新增信用分消息接收成功：" + creditDTO.toString());
        creditManageService.addCredit(creditDTO);
        System.out.println("信用分新增成功");
        return Result.success();
    }

    /**
     * 卡夫卡调用接口
     * @param kafkaDTO
     * @return
     */
    @Transactional
    @PostMapping("/kafka/credit")
    @Operation(summary = "卡夫卡调用接口")
    public Result<String> addCreditKafka(@RequestBody KafkaDTO kafkaDTO){
        System.out.println("消息队列消息接收成功：" + kafkaDTO.toString());
        if(kafkaDTO.getCreditDimensionDTO().getChangeCredit() != kafkaDTO.getRecordDTO().getChangeCredit()){
            return Result.error("传输数据有误，记录与维度更改中的信用分变化应该相同");
        }
        //更新信用分记录
        updateCredit(kafkaDTO.getCreditDimensionDTO());
        //存储守约记录
        addRecord(kafkaDTO.getRecordDTO());
        return Result.success();
    }

    /**
     * 守约记录新增接口
     * @param recordDTO
     * @return
     */
    @PostMapping("/record")
    @Operation(summary = "守约记录新增接口")
    public Result<String> addRecord(@RequestBody RecordDTO recordDTO){
        System.out.println("守约记录接收成功：" + recordDTO.toString());
        recordService.addRecord(recordDTO);
        System.out.println("守约记录新增成功");
        return Result.success();
    }

    /**
     * 信用分更新接口
     * @param creditDimensionDTO
     * @return
     */
    @PutMapping("/credit")
    @Operation(summary = "信用分更新接口")
    public Result<String> updateCredit(@RequestBody CreditDimensionDTO creditDimensionDTO){
        System.out.println("更新信用分接收成功："+creditDimensionDTO.toString());
        Result<CreditInterpretation> interpretationResult = openFeignService.getCreditInterpretation(creditDimensionDTO.getUserId());
        System.out.println("步骤一："+interpretationResult.toString());
        CreditInterpretation creditInterpretation = interpretationResult.getData();
        System.out.println("步骤二："+creditInterpretation.toString());
        System.out.println("步骤四，更新："+creditDimensionDTO.toString());
        creditManageService.updateCredit(creditDimensionDTO, creditInterpretation);
        System.out.println("信用分更新成功");
        return Result.success();
    }

    /**
     * 删除用户信用分
     * @param userId
     * @return
     */
    @DeleteMapping("/credit/{userId}")
    @Operation(summary = "删除用户信用分")
    public Result<String> deleteCredit(@PathVariable("userId") Integer userId){
        creditManageService.deleteCredit(userId);
        return Result.success();
    }

}
